﻿<template>
  <div class="table">
    <!--表2季度生产情况T_MulQuartProduc-->
    <div class="divSearch">
      <Row>
        <Col span="24" class="col-margin"> 填报年度：
        <Select v-model="searchModel.FillInYear" placeholder="全部" style="width: 100px" @on-change="getReportData()">
            <Option value="">全部</Option>
            <Option :value="item.DictText" v-for="item in CreateYearList" :key="item.DictValue">{{item.DictText}}</Option>
        </Select>
        <Button icon="ios-search" type="ghost" @click="getReportData()" style="margin-left: 5px;background: linear-gradient(to bottom, #64c8ef 0%,#00a2e2 100%);color: #fff;border-radius: 3px;text-shadow: 1px 1px 0px rgba(0, 0, 0, 0.3);">
            查询
        </Button>
        <Button icon="ios-download" type="primary" @click="ExcelDown()" class="searchButton" style="float:right;width: 100px;">数据导出</Button>
        <Upload :on-success="handleSuccess" :action="GetPostUrl()" style="float:right;width: 100px;" v-show="ReportStatus <3">
          <Button type="primary" icon="ios-cloud-upload-outline" class="searchButton">数据导入</Button>
        </Upload>
        <Button icon="ios-cloud-download" type="primary" @click="TmpDown()" class="searchButton" style="width: 100px;">模板下载</Button>
        <!-- <label style="float:right;padding: 6px;font-size:16px;">当前状态:
          <span v-if="ReportStatus==0" class="Error"></span> 
          <span v-else-if="ReportStatus==1" class="NoReport">未填写</span>
          <span v-else-if="ReportStatus==2" class="AlreadyReport">已填写</span>
          <span v-else-if="ReportStatus==3" class="UpReport">已上报</span>
          <span v-else-if="ReportStatus==4" class="Success">审核通过</span>               
      </label> -->
        </Col>
      </Row>
    </div>
    <!-- <Row style="padding-left:10px;">
      <Col>
      <Button icon="plus" type="ghost" size="small" @click="addItem" :disabled="ReportStatus < 3 && BtnSaveStatus ? false:true">新增</Button>
      </Col>
    </Row> -->
    <Row>
      <table class="reptable">
        <caption>
            <label style="float:left">{{searchModel.FillInYear}}表10桑园套种作物收益情况填报（抽样调查）</label>
            <label style="float:right">当前状态:
            <Poptip trigger="hover" placement="left-start" v-model="visible" :content="ReportOpinion" v-if="ReportStatus==0">
                  <span  class="Error">重新填写</span> 
                </Poptip>
            <span v-else-if="ReportStatus==1" class="NoReport">未填写</span>
            <span v-else-if="ReportStatus==2" class="AlreadyReport">已填写</span>
            <span v-else-if="ReportStatus==3" class="UpReport">已上报</span>
            <span v-else-if="ReportStatus==4" class="Success">审核通过</span>   
            <span v-else class="NoReport">无需填报</span>                 
            </label><br/>
            <label style="float:left;margin-bottom:5px">
               <Button icon="plus" type="success" size="small" @click="addItem" :disabled="ReportStatus < 3 && BtnSaveStatus ? false:true">新增</Button>
            </label> 
        </caption>    
        </table>
        <div  style="width:100%; overflow:scroll;">
          <table class="ContentTable" style="width:150%;">
            <thead>
              <tr>
                <th rowspan="2">序号</th>
                <th rowspan="2">操作</th>
                <th rowspan="2">农户姓名</th>
                <th rowspan="2">套种作物名称</th>
                <th rowspan="2">播种时间</th>
                <th rowspan="2">收获时间</th>
                <th rowspan="2">套种面积(亩)</th>
                <th colspan="6">套种支出</th>
                <th colspan="2">套种收入</th>
                <th rowspan="2" >平均亩桑套种效益（元/亩）</th>
              </tr>
              <tr>
                <th>用种量(公斤/亩)</th>
                <th>种子价格(元/公斤)</th>
                <th>因套种增加的化肥用量(公斤/亩)</th>
                <th>化肥价格(元/公斤)</th>
                <th>全生育期用工量(个/亩)</th>
                <th>当地工价(元/个)</th>
                <th>收获产量(公斤/亩)</th>
                <th>销售单价(元/公斤)</th>
              </tr>
            </thead>
            <tbody style="max-height:240px;">
              <tr v-for="(dataitem,index) in datalist" :key="dataitem.Id">
                <td>{{index+1}}</td>
                <td>
                  <Button type="ghost" size="small" icon="ios-trash-outline" style="color:#FF0000" @click="delOp(dataitem)" :disabled="ReportStatus < 3 && BtnSaveStatus ? false:true"></Button>
                </td>
                <td>
                  <Input style="display:none;" v-model="dataitem.Id"></Input>
                  <Input style="display:none;" v-model="dataitem.ReportId"></Input>
                  <Input v-model="dataitem.HouseName"></Input>
                </td>
                <td>
                  <!--套种作物名称-->
                  <!-- <Input v-model="dataitem.InterCropName"></Input> -->
                  <Select v-model="dataitem.InterCropName" placeholder="请选择">
                      <Option :value="item.DictValue" v-for="item in InterCropList" :key="item.DictValue">{{item.DictText}}</Option>
                  </Select>
                </td>
                <td>
                  <!-- <Input v-model="dataitem.SowTime"></Input> -->
                  <Date-picker  type="month" placeholder="请选择" v-model="dataitem.SowTime"></Date-picker>
                  <!--播种时间-->
                </td>
                <td>
                  <!-- <Input v-model="dataitem.HarvestTime">{{dataitem.SowTime}}</Input> -->
                  <Date-picker  type="month" placeholder="请选择" v-model="dataitem.HarvestTime"></Date-picker>
                  <!--收获时间-->
                </td>
                <td>
                  <Input v-model="dataitem.InterArea"></Input>
                  <!--套种面积-->
                </td>
                <td>
                  <Input v-model="dataitem.UseSeed"></Input>
                  <!--用种量-->
                </td>
                <td>
                  <Input v-model="dataitem.SeedPrice"></Input>
                  <!--种子价格-->
                </td>
                <td>
                  <Input v-model="dataitem.AddFertili"></Input>
                  <!--化肥量-->
                </td>
                <td>
                  <Input v-model="dataitem.FertilizerPrice"></Input>
                  <!--化肥价格-->
                </td>
                <td
                  <Input v-model="dataitem.FullGrowEmploy"></Input>
                  <!--全生育期用工量-->
                </td>
                <td>
                  <Input v-model="dataitem.LocalWages"></Input>
                  <!--当地工价-->
                </td>
                <td>
                  <Input v-model="dataitem.HarvestYield"></Input>
                  <!--收获产量-->
                </td>
                <td>
                  <Input v-model="dataitem.SalePrice"></Input>
                  <!--销售单价-->
                </td>
                <td  v-if="(Number(dataitem.HarvestYield)*Number(dataitem.SalePrice))-((Number(dataitem.UseSeed)*Number(dataitem.SeedPrice))
                  +(Number(dataitem.AddFertili)*Number(dataitem.FertilizerPrice))+(Number(dataitem.FullGrowEmploy)*Number(dataitem.LocalWages)))>0">
                  {{(Number(dataitem.HarvestYield)*Number(dataitem.SalePrice))-((Number(dataitem.UseSeed)*Number(dataitem.SeedPrice))
                  +(Number(dataitem.AddFertili)*Number(dataitem.FertilizerPrice))+(Number(dataitem.FullGrowEmploy)*Number(dataitem.LocalWages))) | decimal}}
                </td>
                <td style="color:red" v-else-if="(Number(dataitem.HarvestYield)*Number(dataitem.SalePrice))-((Number(dataitem.UseSeed)*Number(dataitem.SeedPrice))
                  +(Number(dataitem.AddFertili)*Number(dataitem.FertilizerPrice))+(Number(dataitem.FullGrowEmploy)*Number(dataitem.LocalWages)))<=0">
                  {{(Number(dataitem.HarvestYield)*Number(dataitem.SalePrice))-((Number(dataitem.UseSeed)*Number(dataitem.SeedPrice))
                  +(Number(dataitem.AddFertili)*Number(dataitem.FertilizerPrice))+(Number(dataitem.FullGrowEmploy)*Number(dataitem.LocalWages))) | decimal}}
                </td>
                <td v-else>0.0</td>
              </tr>
            </tbody> 
          </table>
        </div>
      <div class="divButton">
        <Button type="primary" @click="save()" :disabled="ReportStatus < 3 && BtnSaveStatus ? false:true">保存</Button>
        <Button type="primary" @click="DataUp()" :disabled="ReportStatus == 2 ? false:true">上报</Button>
      </div>
      <Alert banner type="error">
        <p style="color:red;font-size:18px">填报说明:</p>
        <p>1、用工量指因套种作物而增加的工时,含蚕农自己投工量；</p>
        <!-- <p>2、在一个养蚕周期内，被调查对象在桑园内进行的套种作物均要进行问卷调查。</p> -->
        <p>2、销售单价为套种作物成熟季节，市场上的卖价。</p>
        <!-- <p>4、平均亩桑套种效益=收获产量*销售单价-（用种量*种子价格+因套种增加的化肥用量*化肥价格+全生育期用工量*当地工价）</p> -->
        <p style="color:red;font-size: 16px;padding-top: 10px;">特别注意：若指标项没有数据，请直接填“0”</p>
        <span slot="desc"></span>
      </Alert>
      </Col>
    </Row>
  </div>
</template>

<script>
  export default {
    data() {
      const validateNumber = (rule, value, callback) => {
        if (!Number.isInteger(value)) {
          callback(new Error('请输入数字值'));
        } else {
          if (value < 18) {
            callback(new Error('必须年满18岁'));
          } else {
            callback();
          }
        }
      };
      return {
        visible:true,
        ReportOpinion:'',
        datalist: [],
        datas: {}, //table数据对象
        TableCodeList: this.GetGroupDict("ReportTableName"), //表名称
        CreateYearList: this.GetGroupDict("ReportYear"), //上报年份
        CurrentYear: this.GetCurrentYear(), //当前年度
        Month:new Date().getFullYear() + "-" + (new Date().getMonth()+1),
        InterCropList:this.GetGroupDict("InterCrop"),
        ReportId: '',
        ReportStatus: '',
        BtnSaveStatus:false,
        //查询模型：根据需求删除不不要查询条件
        searchModel: {
          ReportCode: 'MulCropIncome',
          FillInYear: '',
          TimesType: '',
          AreaCode: this.GetlocaStorage("CSuserinfo").AreaCode,
        },
        clientHeight: window.innerHeight - 300, //表格默认高度
      }
    },
    mounted() {
      //table高度计算方法
      const that = this;
      window.onresize = () => {
        return (() => {
          that.clientHeight = window.innerHeight - 300;
        })()
      };
    },
    methods: {
      addItem() {
        var model = { //编辑模型
          Id: 0,
          ReportId: '',
          SiteInter: '',
          InterCropName: '',
          SowTime: '',
          HarvestTime: '',
          InterArea: '',
          SeedPrice: '',
          UseSeed: '',
          AddFertili: '',
          fertilizerPerMu: '',
          LocalWages: '',
          FullGrowEmploy: '',
          HarvestYield: '',
          SalePrice: '',
          AveIncome: '',
        };
        model.ReportId = this.ReportId;
        model.Id = this.datalist.length + 1;
        model.SowTime = this.Month;
        this.datalist.push(model);
      },
      delOp(m) {
        for (var i = 0; i < this.datalist.length; i++) {
          if (this.datalist[i].Id == m.Id) {
            this.datalist.splice(i, 1);
          }
        }
      },
      //数据上报
      DataUp() {
        var self = this;
        self.$Modal.confirm({
          title: '提示信息',
          content: '<p>数据上报后，不能再更改，是否继续上报？</p>',
          onOk: () => {
            var model = new Object();
            model.Id = self.ReportId;
            model.reportType = '3';
            model.strMsg = '';
            this.$http.post("Report/PostUpStatus", model).then(
              function (res) {
                if (res.data.status == 1) {
                  self.$Notice.success({
                    title: '数据上报成功！',
                    desc: false
                  });
                  self.getReportData();
                } else {
                  self.$Modal.error({
                    title: "提示信息",
                    content: "数据上报失败!"
                  });
                }
              }
            );
          }
        });
      },
      getReportData() {
        var self = this;
        var user = this.GetlocaStorage("CSuserinfo");
        this.searchModel.AreaCode = user.AreaCode;
        if (self.searchModel.FillInYear != null && self.searchModel.FillInYear != '') {
          this.searchModel.FillInYear = self.searchModel.FillInYear;
        } else {
          this.searchModel.FillInYear = self.CurrentYear + '年'; //填报年度-----数据字典值
        }
        self.$http.post('Report/PostPageList', this.searchModel).then(function (res) {
          if (res.data.status === 1) {
            if (res.data.rows.length > 0) {
              self.ReportId = res.data.rows[0].Id;
              self.ReportStatus = res.data.rows[0].Status;
              self.BtnSaveStatus = true;
            } else {
              self.ReportId = '';
              self.ReportStatus = '';
              self.BtnSaveStatus = false;
            }
            self.getPageData(self.ReportId);
          } else {
            self.$Modal.error({
              title: '提示信息',
              content: res.data.message
            });
          }
        });
      },
      //获取this.datas: {},//table数据对象
      getPageData(ReportId) {
        var self = this;
        var reportCode = self.searchModel.ReportCode;
        this.$http.get("Report", {
          params: {
            id: ReportId,
            reportCode: reportCode
          }
        }).then( //获取数据编辑项的值
          function (res) {
            switch (res.data.list.status) {
              case 1:
                self.ReportOpinion=res.data.Opinion;
                self.datalist = res.data.list.model;
                break;
              case 0:
                self.$Modal.error({
                  title: "提示信息",
                  content: "数据加载失败!!!"
                });
                break;
              case -1:
                self.$Modal.error({
                  title: "提示信息",
                  content: "数据加载失败!!!"
                });
                break;
            }
          }
        );
      },
      //检查一位小数
      checkOne(value){
        var reg = /\S/;
        var temp = /^[0-9]\d*(\.\d+)?$/;//数字验证
        var p=/^[0-9]+([.]{1}[0-9]{1})?$/;//匹配整数或带一个小数
        if (reg.test(value)) {
          if (temp.test(value) == false) 
          {
            return false;
          }
          else {
            if(p.test(value)==false)
            {
              return false;
            }
            else{
              return true;
            }
          }
        } 
        else {
          return false;
        }
      },
      //检查空
      checkEmpty(value){
        var reg = /\S/;
        if (reg.test(value)) {
          return true;
        } 
        else {
          return false;
        }
      },
      //保存方法
      save() {
        var self = this;
        var m=0;
        for(var i=0;i<self.datalist.length;i++)
        {  
          if(self.checkEmpty(self.datalist[i].HouseName)&&self.checkEmpty(self.datalist[i].InterCropName)&&self.checkEmpty(self.datalist[i].SowTime)
          &&self.checkEmpty(self.datalist[i].HarvestTime)&&self.checkOne(self.datalist[i].InterArea)
          &&self.checkOne(self.datalist[i].SeedPrice)&&self.checkOne(self.datalist[i].UseSeed)
          &&self.checkOne(self.datalist[i].AddFertili)&&self.checkOne(self.datalist[i].LocalWages)
          &&self.checkOne(self.datalist[i].FullGrowEmploy)&&self.checkOne(self.datalist[i].HarvestYield)
          &&self.checkOne(self.datalist[i].SalePrice))
          {
            m+=1;
          }
        }
        if(m==self.datalist.length)
        {
          this.$http.post('MulCropIncome', self.datalist).then(
            function (res) {
              if (res.data.status == 1) {
                self.getReportData();
                self.$Notice.success({
                  title: '保存成功！',
                  desc: false
                });
              } else {
                self.$Modal.warning({
                  title: "提示信息",
                  content: "更新失败,请与管理员联系!"
                });
              }
          });
        }else{
          this.$Notice.error({title: '数据有误,请检查后再试!',desc: '',});
        }
      },
      //导入
      GetPostUrl() {
        var url = this.$http.defaults.baseURL + "Report/PostImportData?FillInYear="+this.searchModel.FillInYear+"&&ReportCode=" + this.searchModel.ReportCode +
          "&&AreaCode=" + this.searchModel.AreaCode;
        return url;
      },
      handleSuccess(res, file) {
        var self = this;
        self.getReportData();
        self.$Notice.success({
          title: '数据导入成功！',
          desc: false
        });
      },
      //导出
      ExcelDown: function () {
        var self = this;
        var model = new Object();
        model.ReportCode = self.searchModel.ReportCode;
        if (self.searchModel.FillInYear != null && self.searchModel.FillInYear != '') {
          model.FillInYear = self.searchModel.FillInYear;
        } else {
          model.FillInYear = self.CurrentYear + '年'; //填报年度-----数据字典值
          self.searchModel.FillInYear = self.CurrentYear + '年';
        }
        model.AreaCode = self.searchModel.AreaCode;
        window.open(this.$http.defaults.baseURL + "MulCropIncome/GetExcelData?ReportCode=" + model.ReportCode +
          "&&FillInYear=" + model.FillInYear + "&&AreaCode=" + model.AreaCode);
      },
      //模板下载
      TmpDown: function () {
        window.open(this.$http.defaults.baseURL + "Report/GetTemplateData?ReportCode=" + this.searchModel.ReportCode);
      },
    },
    //创建Vue页面方法
    created: function () {
      //创业Vue前调用获取this.datas: {},//table数据对象
      this.getReportData();
    },
    watch: {
      //需要监听的在此处新增监听对象
    }
  }

</script>
<style scoped src="../../../styles/stats.css"></style>